package com.mnxc.gxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mnxc.gxa.dto.Options;
import com.mnxc.gxa.dto.QueryMenuDTO;
import com.mnxc.gxa.dto.Result;
import com.mnxc.gxa.entity.Menu;
import com.mnxc.gxa.entity.User;
import com.mnxc.gxa.mapper.MenuMapper;
import com.mnxc.gxa.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mnxc
 * @since 2023-06-29
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Resource
    MenuMapper menuMapper;

    @Resource
    IMenuService menuService;

    @Override
    public Result getAll() {
        // 1.获取数据
        List<Menu> menus = menuService.list();
        return Result.ok(menus, (long) menus.size());
    }

    @Override
    public Result getByPageAndCondition(int page, int pageSize, QueryMenuDTO query) {
        //1.构造分页器
        IPage<Menu> pageInfo = new Page<>(page,pageSize);
        //2.条件查询
        LambdaQueryWrapper<Menu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(query.getStatus()!=null,Menu::getStatus,query.getStatus())
                .like(query.getTitle()!=null,Menu::getTitle,query.getTitle())
                .like(query.getUrl()!=null,Menu::getUrl,query.getUrl());
        //查询基本信息
        this.page(pageInfo,lqw);
        return Result.ok(pageInfo);
    }

    @Override
    public Result saveMenu(Menu menu){
        // 1.保存用户信息
        boolean isSuccess = save(menu);
        if(!isSuccess){
            // 2.保存失败，返回报错信息
            return Result.fail("添加菜单失败！");
        }
        // 3.保存成功
        return Result.ok();
    }

    @Override
    public Result activeMenu(Menu menu) {
        boolean isSuccess = menuMapper.activeMenu(menu);

        if (!isSuccess) {
            Result.fail("激活用户失败！");
        }
        return Result.ok("激活用户成功！");
    }

    @Override
    public Result freezeMenu(Menu menu) {
        boolean isSuccess = menuMapper.freezeMenu(menu);
        if (!isSuccess) {
            Result.fail("冻结用户失败！");
        }
        return Result.ok("冻结用户成功！");
    }

    @Override
    public Result deleteMenu(Menu menu) {
        boolean isSuccess = menuMapper.deleteMenu(menu);
        if (!isSuccess) {
            Result.fail("删除用户失败！");
        }
        return Result.ok("删除用户成功！");
    }

    @Override
    public Result updateMenu(Menu menu) {
        int isSuccess = menuMapper.updateById(menu);
        if (isSuccess==0) {
            return Result.fail("更新用户信息失败！");
        }
        return Result.ok("更新用户信息成功！");
    }

    @Override
    public Result getMenusTree() {
        //1.获取菜单列表
        List<Menu> menuList = menuMapper.getAll();
        //2.获取菜单的树状结构
        List<Options<Menu>> menuOptions = new UserServiceImpl().getMenuOptions(menuList);
        return Result.ok(menuOptions);
    }
}
